<?php
//配送员管理
namespace Home\Controller;
class DeliveryController extends CommonController {
    //遍历所处理过的订单（分页，搜索：按时间、订单号和状态搜）
	public function index(){
		$mod = M("users");
		$mod1 = M("indent");
		$mod2 = M("delivery");
		$mod3 = M("shop");
		$mod4 = M("menu");
		//取到当前用户,配送员;
		$se = session("homeuser");
		$user = $mod->where("state=4")->find($se['id']);
		$did = $user['id'];
		// 根据配送员id找到配送员表对应的信息
		$del = $mod2->where("uid=".$did)->find();
		// 找到该配送员的所有处理结束的订单
		$ind = $mod1->where("did=".$del['id']." AND state=5 AND visable=1")->select();
		// 找到该订单对应的用户名 店铺名
		// var_dump($ind);
		foreach ($ind as $k => $v) {
			// 用户名
			$user = $mod->find($v['uid']);
			$ind[$k]['username']=$user['username'];
			// 店铺名
			$shop = $mod3->find($v['sid']);
			$ind[$k]['shopname']=$shop['shopname'];
			$ind[$k]['shopname']=$shop['address'];

			$meid = $mod4->find($v["mid"]);
			$ind[$k]['menu']=$$meid['menu'];

		}	
		$this->assign("same",$ind);
		$this->display("Delivery/deliInfo");
    }
	
	//未处理定单
	public function accept(){
		$mod = M("users");
		$mod1 = M("indent");
		$mod2 = M("delivery");
		$mod3 = M("shop");
		$mod4 = M("menu");

		//取到当前用户,配送员;
		$se = session("homeuser");
		$user = $mod->where("state=4")->find($se['id']);
		$did = $user['id'];
		// 根据配送员id找到配送员表对应的信息
		$del = $mod2->where("uid=".$did)->find();
		// 找到所有商家准备配送的订单
		$ind = $mod1->where("state=3 AND did IS NULL")->select();		
		foreach ($ind as $k => $v) {
			// 用户名
			$user = $mod->find($v['uid']);
			$ind[$k]['username']=$user['username'];
			// 店铺名
			$shop = $mod3->find($v['sid']);
			$ind[$k]['shopname']=$shop['shopname'];
			$ind[$k]['address']=$shop['address'];

			$meid = $mod4->find($v["mid"]);
			$ind[$k]['menu']=$meid['menu'];

		}	
		//配送员的area字段
		$darea = $del["area"];
		$same = array();
		foreach($ind as $k=>$v){
			if($ind[$k]["address"] == $darea){
				$same[] = $ind[$k];
			}
			
		}
		$this->assign("same",$same);
		$this->display("Delivery/waitInfo");
	}
	// 已接收订单
	public function jieshou(){
		$mod = M("users");
		$mod1 = M("indent");
		$mod2 = M("delivery");
		$mod3 = M("shop");
		$mod4 = M("menu");

		//取到当前用户,配送员;
		$se = session("homeuser");
		$user = $mod->where("state=4")->find($se['id']);
		$did = $user['id'];
		// 根据配送员id找到配送员表对应的信息
		$del = $mod2->where("uid=".$did)->find();
		// 找到所有商家准备配送的订单
		$ind = $mod1->where("state=4")->select();		
		foreach ($ind as $k => $v) {
			// 用户名
			$user = $mod->find($v['uid']);
			$ind[$k]['username']=$user['username'];
			// 店铺名
			$shop = $mod3->find($v['sid']);
			$ind[$k]['shopname']=$shop['shopname'];
			$ind[$k]['address']=$shop['address'];

			$meid = $mod4->find($v["mid"]);
			$ind[$k]['menu']=$meid['menu'];

		}	
		//配送员的area字段
		$darea = $del["area"];
		$same = array();
		foreach($ind as $k=>$v){
			if($ind[$k]["address"] == $darea){
				$same[] = $ind[$k];
			}
			
		}
		$this->assign("same",$same);
		$this->display("Delivery/jiedan");
	}
	
	//执行接单
	public function doAccept(){
		$mod = M("indent");
		$ind = $_GET["id"];
		// 当前用户
		$se = session("homeuser");
		$user = M("users")->where("state=4")->find($se['id']);
		$did = $user['id'];
		// 根据配送员id找到配送员表对应的信息
		$del = M("delivery")->where("uid=".$did)->find();
		// 更改数据
		$data['did'] = $del['id'];
		$data['state'] = 4;
		$mod->where("id = '".$ind."'")->save($data); // 根据条件保存修改的数据
		$this->redirect("Delivery/jieshou");
		//$this->success('接单完成','Delivery/index');
	}
	
	//送达
	public function served(){
		$mod = M("indent");
		$ind = $_GET["id"];
		
		$data['state'] = 5;
		$data['end_time'] = time();
		$m = $mod->where("id = '".$ind."'")->save($data); // 根据条件保存修改的数据
		$this->redirect("Delivery/index");
	}
	
	//删除订单（只能删自己看到的，不能删订单库里的）
	public function delOrder(){
		//实例化model类
		$mod = M("indent");
		$ind = $_GET["id"];
		$data['visable'] = "0";
		$m = $mod->where("id = '".$ind."'")->save($data); // 根据条件保存修改的数据
		$data = array();
        if($m>0){
            $data['b'] = true;
        }else{
            $data['b']= false;
        }
        $this->ajaxReturn($data);
	}
}